
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.7.Pod 标签 · Kubernetes - 痴者工良</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="痴者工良">
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="8.schedule.html" />
    
    
    <link rel="prev" href="6.scale.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://www.whuanle.cn" target="_blank" class="custom-link">痴者工良的博客</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    文档说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    1.基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../1.basic/">
            
                <a href="../1.basic/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../1.basic/1.docker.html">
            
                <a href="../1.basic/1.docker.html">
            
                    
                    1.1 说透 Docker：基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../1.basic/2.virtual.html">
            
                <a href="../1.basic/2.virtual.html">
            
                    
                    1.2 说透 Docker： 虚拟化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../1.basic/3.docker_network.html">
            
                <a href="../1.basic/3.docker_network.html">
            
                    
                    1.3 了解 Docker 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../1.basic/4.pod_docker.html">
            
                <a href="../1.basic/4.pod_docker.html">
            
                    
                    1.4 Docker 和 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../1.basic/5.k8s.html">
            
                <a href="../1.basic/5.k8s.html">
            
                    
                    1.5 K8S入门基础
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../2.deploy/">
            
                <a href="../2.deploy/">
            
                    
                    2.部署和配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../2.deploy/2.deploy.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../2.deploy/1.minikube.html">
            
                <a href="../2.deploy/1.minikube.html">
            
                    
                    2.1 使用 Minikube 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../2.deploy/2.kubeadm.html">
            
                <a href="../2.deploy/2.kubeadm.html">
            
                    
                    2.2 使用 kubeadm 部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../2.deploy/3.kubeadm_ckad.html">
            
                <a href="../2.deploy/3.kubeadm_ckad.html">
            
                    
                    2.3 CKAD认证中的部署教程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../2.deploy/4.kubeadm_proxy.html">
            
                <a href="../2.deploy/4.kubeadm_proxy.html">
            
                    
                    2.4 国内代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../2.deploy/5.dashboard.html">
            
                <a href="../2.deploy/5.dashboard.html">
            
                    
                    2.5 Dashboard
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="./">
            
                <a href="./">
            
                    
                    3.Pod部署和调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="./">
            
                <a href="./">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="1.pod.html">
            
                <a href="1.pod.html">
            
                    
                    3.1 Pod
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="2.deployment.html">
            
                <a href="2.deployment.html">
            
                    
                    3.2 Deployment部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="3.replica.html">
            
                <a href="3.replica.html">
            
                    
                    3.3 副本集(ReplicaSet)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="4.pod_network.html">
            
                <a href="4.pod_network.html">
            
                    
                    3.34 Pod 端口映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="5.update.html">
            
                <a href="5.update.html">
            
                    
                    3.5 Pod 升级、回滚
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="6.scale.html">
            
                <a href="6.scale.html">
            
                    
                    3.6 Pod 缩放
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.8" data-path="7.lable.html">
            
                <a href="7.lable.html">
            
                    
                    3.7.Pod 标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="8.schedule.html">
            
                <a href="8.schedule.html">
            
                    
                    3.8 Pod 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="9.jobs_cronjobs.html">
            
                <a href="9.jobs_cronjobs.html">
            
                    
                    3.9 Jobs、CronJobs
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    4.Kubernetes 网络
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../4.network/">
            
                <a href="../4.network/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../4.network/1.network.html">
            
                <a href="../4.network/1.network.html">
            
                    
                    4.1 Kubernetes 网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../4.network/2.endpoint.html">
            
                <a href="../4.network/2.endpoint.html">
            
                    
                    4.2 Endpoint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../4.network/3.ingress.html">
            
                <a href="../4.network/3.ingress.html">
            
                    
                    4.3 ingress
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../4.network/4.discovery.html">
            
                <a href="../4.network/4.discovery.html">
            
                    
                    4.4 服务发现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../5.volumes/">
            
                <a href="../5.volumes/">
            
                    
                    5.volumes
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../5.volumes/">
            
                <a href="../5.volumes/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../5.volumes/1.volumes.html">
            
                <a href="../5.volumes/1.volumes.html">
            
                    
                    5.1 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../5.volumes/2.secret_configmap.html">
            
                <a href="../5.volumes/2.secret_configmap.html">
            
                    
                    5.2 secret 和 ConfigMap 卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../5.volumes/3.nfts.html">
            
                <a href="../5.volumes/3.nfts.html">
            
                    
                    5.3 NFS卷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../5.volumes/4.pv_pvc.html">
            
                <a href="../5.volumes/4.pv_pvc.html">
            
                    
                    5.4 持久化卷
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    6.API 和 安全
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../6.api_safe/">
            
                <a href="../6.api_safe/">
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../7.health/">
            
                <a href="../7.health/">
            
                    
                    7.维护和故障排除
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../8.health/README.md">
            
                <span>
            
                    
                    导读
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../k8s.html">
            
                <a href="../k8s.html">
            
                    
                    Kubernetes 命令概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../ckad.html">
            
                <a href="../ckad.html">
            
                    
                    CKAD 认证帮助
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.7.Pod 标签</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#37-&#x6807;&#x7B7E;"><b></b>3.7 &#x6807;&#x7B7E;</a></li><ul><li><span class="title-icon "></span><a href="#&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;"><b></b>&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;</a></li><ul><li><span class="title-icon "></span><a href="#&#x6807;&#x7B7E;"><b></b>&#x6807;&#x7B7E;</a></li><li><span class="title-icon "></span><a href="#&#x8BBE;&#x7F6E;-label"><b></b>&#x8BBE;&#x7F6E; Label</a></li><li><span class="title-icon "></span><a href="#&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;"><b></b>&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;</a></li><li><span class="title-icon "></span><a href="#&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;"><b></b>&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;</a></li><li><span class="title-icon "></span><a href="#selector"><b></b>selector</a></li><li><span class="title-icon "></span><a href="#&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;"><b></b>&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;</a></li></ul><li><span class="title-icon "></span><a href="#&#x6CE8;&#x89E3;"><b></b>&#x6CE8;&#x89E3;</a></li></ul></ul></div><h1 id="37-&#x6807;&#x7B7E;"><a name="37-&#x6807;&#x7B7E;" class="anchor-navigation-ex-anchor" href="#37-&#x6807;&#x7B7E;"><i class="fa fa-link" aria-hidden="true"></i></a>3.7 &#x6807;&#x7B7E;</h1>
<p>&#x5728;&#x524D;&#x9762;&#x7684;&#x5B66;&#x4E60;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B66;&#x5230;&#x4E86; Deployment &#x90E8;&#x7F72;&#xFF0C;&#x4EE5;&#x53CA;&#x526F;&#x672C;&#x6570;(ReplicaSet)&#xFF0C;&#x4F46;&#x662F; Pod &#x90E8;&#x7F72;&#x5230;&#x54EA;&#x4E2A; Worker &#x8282;&#x70B9;&#x662F;&#x968F;&#x673A;&#x7684;&#xFF0C;&#x5373;&#x4F7F;&#x6709; 3&#x4E2A; Woker &#x548C; 3&#x4E2A;  Pod &#x526F;&#x672C;&#xFF0C;&#x4E0D;&#x4E00;&#x5B9A;&#x6BCF;&#x4E2A; Node &#x521A;&#x521A;&#x597D;&#x8FD0;&#x884C;&#x4E00;&#x4E2A; Pod&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x5176;&#x4E2D;&#x4E00;&#x4E2A; Node &#x8FD0;&#x884C;&#x7740;&#x4E09;&#x4E2A;&#x526F;&#x672C;&#x3002;&#x5F53;&#x7136;&#x4F7F;&#x7528; Daemont &#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x6BCF;&#x4E2A; Node &#x5E73;&#x5747;&#x8FD0;&#x884C;&#x8FD9;&#x79CD;&#x4E00;&#x4E2A; Pod &#x526F;&#x672C;&#x3002;&#x4F46;&#x662F;&#x6709;&#x7684;&#x673A;&#x5668;&#x5185;&#x5B58;&#x5927;&#xFF0C;&#x6211;&#x60F3;&#x5C3D;&#x91CF;&#x5C06; Redis Pod &#x8C03;&#x5EA6;&#x5230;&#x6B64;&#x8282;&#x70B9;&#xFF1B;&#x800C;&#x6709;&#x7684;&#x670D;&#x52A1;&#x5668;&#x78C1;&#x76D8;&#x5BB9;&#x91CF;&#x5927;&#xFF0C;&#x6211;&#x60F3;&#x5C3D;&#x53EF;&#x80FD;&#x5C06;&#x65E5;&#x5FD7;&#x670D;&#x52A1;&#x7B49;&#x8C03;&#x5EA6;&#x5230;&#x8FD9;&#x4E9B;&#x8282;&#x70B9;&#xFF0C;&#x90A3;&#x4E48;&#x8BE5;&#x5982;&#x4F55;&#x5904;&#x7406;&#x5462;&#xFF1F;&#x4E3A;&#x4EC0;&#x4E48; master &#x4E0D;&#x4F1A;&#x90E8;&#x7F72;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684; Pod &#x5462;&#xFF1F;</p>
<p>&#x5728;&#x672C;&#x7BC7;&#x6211;&#x4EEC;&#x5C06;&#x63A2;&#x7A76; Kubernetes &#x4E2D;&#x7684; DaemonSet&#x3001;&#x5BB9;&#x5FCD;&#x5EA6;&#x3001;&#x4EB2;&#x548C;&#x6027;&#x3001;Label&#x3001;&#x9009;&#x62E9;&#x5668;&#x7B49;&#x6982;&#x5FF5;&#xFF0C;&#x4EE5;&#x4FBF;&#x63A7;&#x5236; Pod &#x7684;&#x8C03;&#x5EA6;&#x3002;</p>
<h2 id="&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;"><a name="&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;" class="anchor-navigation-ex-anchor" href="#&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6807;&#x7B7E;&#x548C;&#x9009;&#x62E9;&#x5668;</h2>
<h3 id="&#x6807;&#x7B7E;"><a name="&#x6807;&#x7B7E;" class="anchor-navigation-ex-anchor" href="#&#x6807;&#x7B7E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6807;&#x7B7E;</h3>
<p>&#x6807;&#x7B7E;&#x4E3B;&#x8981;&#x662F;&#x7528;&#x4E8E;&#x8868;&#x793A;&#x5BF9;&#x7528;&#x6237;&#x6709;&#x610F;&#x4E49;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x6807;&#x8BC6;&#x3002;</p>
<p>&#x6807;&#x7B7E;(Label)&#x662F;&#x9644;&#x52A0;&#x5230; Kubernetes &#x5BF9;&#x8C61;&#x4E0A;&#x7684;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x5728;&#x5BF9;&#x8C61;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&#x4E2D;&#x4EE5; <code>Labels</code> &#x5B57;&#x6BB5;&#x4FDD;&#x5B58;&#xFF0C;&#x4F8B;&#x5982; Pod &#x53EF;&#x901A;&#x8FC7; <code>kubectl describe pods</code> &#x67E5;&#x8BE2;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6BCF;&#x4E2A; Pod &#x90FD;&#x5E26;&#x6709; <code>Labels: app=...</code>&#x3002;</p>
<pre class="language-"><code>... ...
Labels:       app=nginx
              pod-template-hash=85b45874d9
... ...
</code></pre><blockquote>
<p><strong>[Info] &#x63D0;&#x793A;</strong></p>
<p>&#x6BCF;&#x4E2A; Pod &#x90FD;&#x6709; app &#x6807;&#x7B7E;&#xFF0C;&#x8868;&#x793A; Pod &#x6A21;&#x677F;&#x7684;&#x540D;&#x79F0;&#xFF0C;app &#x6807;&#x7B7E;&#x7684;&#x503C;&#x8DDF; Pod &#x6A21;&#x677F;&#x540D;&#x79F0;&#x4E00;&#x81F4;&#x3002;</p>
<p>&#x5F53;&#x6211;&#x4EEC;&#x521B;&#x5EFA; Deployment &#x65F6;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6307;&#x5B9A; Pod &#x7684; app &#x6807;&#x7B7E;&#x503C;&#xFF0C;&#x90A3;&#x4E48;&#x4E00;&#x822C;&#x8DDF; Deployment &#x540D;&#x79F0;&#x4E00;&#x81F4;&#x3002;&#x5982; <code>kubectl create deployment nginx  --image=nginx:latest</code> &#x521B;&#x5EFA;&#x7684; Pod&#xFF0C;&#x5176; app &#x6807;&#x7B7E;&#x503C;&#x5C31;&#x662F; nginx&#x3002;</p>
<p>Deployment &#x3001;ReplicaSet &#x7B49;&#x90FD;&#x662F;&#x901A;&#x8FC7; Pod &#x7684; app &#x6807;&#x7B7E;&#x8BC6;&#x522B; Pod &#x662F;&#x4E0D;&#x662F;&#x5C5E;&#x4E8E;1&#x81EA;&#x5DF1;&#x7BA1;&#x63A7;&#x7684;&#x3002;</p>
</blockquote>
<p>&#x5728; <code>kubectl get</code> &#x540E;&#x9762;&#x52A0;&#x4E0A; <code>--show-labels</code> &#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x7B5B;&#x9009;&#x83B7;&#x5F97;&#x5BF9;&#x8C61;&#x7684; Label &#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl get pods --show-labels
</code></pre>
<pre class="language-"><code class="lang-text">NAME                     READY   STATUS    RESTARTS   AGE     LABELS
nginx-55495b586f-4hlzm   1/1     Running   0          4h16m   app=nginx,pod-template-hash=55495b586f
nginx-55495b586f-5m985   1/1     Running   0          4h16m   app=nginx,pod-template-hash=55495b586f
nginx-55495b586f-cs4zh   1/1     Running   0          4h16m   app=nginx,pod-template-hash=55495b586f
... ...
</code></pre>
<p>&#x5728;&#x5BF9;&#x8C61;&#x7684; YAML &#x6587;&#x4EF6;&#x4E2D;&#xFF0C;<code>metadata.label</code> &#x5B58;&#x50A8;&#x4E86;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x6807;&#x7B7E;&#x5143;&#x6570;&#x636E;&#x3002;</p>
<p>&#x5982;&#x679C;&#x7528; JSON &#x8868;&#x793A;&#x9644;&#x52A0;&#x5230; metadata &#x7684; label&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token string">&quot;metadata&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token string">&quot;labels&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;key1&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;value1&quot;</span><span class="token punctuation">,</span>
    <span class="token string">&quot;key2&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;value2&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5982;&#x679C;&#x7528; YAML &#x8868;&#x793A;&#x9644;&#x52A0;&#x5230; metadata &#x7684; label</p>
<pre class="language-"><code class="lang-YAML">metadata:
  labels:
    key1: &quot;value1&quot;
    key2: &quot;value2&quot;
</code></pre>
<blockquote>
<p>&#x4F7F;&#x7528; <code>kubectl get pod {&#x540D;&#x79F0;} -o yaml</code> &#x67E5;&#x8BE2;&#xFF0C;&#x6216;&#x8005;&#x4F7F;&#x7528; <code>kubectl edit pod {&#x540D;&#x79F0;}</code> &#x67E5;&#x770B;&#x4FEE;&#x6539; Pod &#x7684; &#x6807;&#x7B7E;&#x3002;</p>
</blockquote>
<p>&#x5728; Deployment &#x4E2D;&#xFF0C;selector &#x5B57;&#x6BB5;&#x8BBE;&#x7F6E;&#x4E86;&#x5982;&#x4F55;&#x67E5;&#x627E;&#x5C5E;&#x4E8E;&#x6B64;&#x5BF9;&#x8C61;&#x7684; Pod&#x3002;</p>
<pre class="language-"><code class="lang-yaml">  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
</code></pre>
<p>Label &#x53EF;&#x4EE5;&#x5728;&#x591A;&#x4E2A;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#xFF0C;&#x4F8B;&#x5982;&#x5728; Node &#x4E0A;&#x6DFB;&#x52A0; Label&#xFF0C;&#x6807;&#x8BC6;&#x6B64; Node&#xFF1B;&#x800C;&#x5728; NodeSelector &#x91CC;&#x4F7F;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x5408;&#x9002;&#x7684; Node &#x8FD0;&#x884C; Pod&#xFF1B;&#x5728; <code>metadata</code> &#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x5BF9;&#x5143;&#x6570;&#x636E;&#x52A0;&#x4EE5;&#x63CF;&#x8FF0;&#x3002;&#x5728; metadata &#x4E2D;&#x6DFB;&#x52A0;&#x7684; Label&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x547D;&#x4EE4;&#x67E5;&#x8BE2;&#x65F6;&#x505A;&#x7B5B;&#x9009;&#x3002;</p>
<h3 id="&#x8BBE;&#x7F6E;-label"><a name="&#x8BBE;&#x7F6E;-label" class="anchor-navigation-ex-anchor" href="#&#x8BBE;&#x7F6E;-label"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8BBE;&#x7F6E; Label</h3>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x624B;&#x52A8;&#x7ED9;&#x4E00;&#x4E2A; Node &#x6216;&#x8005;&#x522B;&#x7684;&#x5BF9;&#x8C61; &#x6DFB;&#x52A0;&#x6807;&#x7B7E;&#x3002;</p>
<pre class="language-"><code class="lang-bash">kubectl label nodes <span class="token operator">&lt;</span>node-name<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>label-key<span class="token operator">&gt;=</span><span class="token operator">&lt;</span>label-value<span class="token operator">&gt;</span>
kubectl label pod <span class="token punctuation">{</span>pod&#x540D;&#x79F0;<span class="token punctuation">}</span> <span class="token operator">&lt;</span>node-name<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>label-key<span class="token operator">&gt;=</span><span class="token operator">&lt;</span>label-value<span class="token operator">&gt;</span>
</code></pre>
<p>&#x4F8B;&#x5982;&#x6211;&#x4EEC;&#x7ED9;&#x4E00;&#x4E2A; Pod &#x8BBE;&#x7F6E;&#x4E00;&#x4E2A; <code>disksize</code>&#xFF0C;&#x8868;&#x793A; Pod &#x9700;&#x8981;&#x5F88;&#x5927;&#x7684;&#x786C;&#x76D8;&#x3002;</p>
<pre class="language-"><code class="lang-bash">kubectl label pod nginx-55495b586f-4hlzm <span class="token assign-left variable">disksize</span><span class="token operator">=</span>big
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x5220;&#x9664;&#x4E00;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5728;&#x6807;&#x7B7E;&#x540D;&#x5B57;&#x540E;&#x9762;&#x52A0;&#x4E0A; <code>-</code> &#x3002;</p>
<pre class="language-"><code class="lang-shell">kubectl label pod nginx-55495b586f-4hlzm disksize-
</code></pre>
<p>&#x5982;&#x679C;&#x6807;&#x7B7E;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#xFF0C;&#x9700;&#x8981;&#x8986;&#x76D6;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl label pod nginx-55495b586f-4hlzm <span class="token assign-left variable">disksize</span><span class="token operator">=</span>big --overwrite
</code></pre>
<p>&#x5728; kube-system &#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;&#xFF0C;&#x8FD0;&#x884C;&#x7740; Kubernetes &#x7684;&#x6838;&#x5FC3;&#x7EC4;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x6B64;&#x547D;&#x540D;&#x7A7A;&#x95F4;&#x4E2D;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x7684; Label&#x3002;</p>
<pre class="language-"><code class="lang-shell">kubectl get nodes --namespace<span class="token operator">=</span>kube-system --show-labels
</code></pre>
<pre class="language-"><code class="lang-text">beta.kubernetes.io/arch=amd64,
beta.kubernetes.io/os=linux,
kubernetes.io/arch=amd64,
... ...
</code></pre>
<h3 id="&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;"><a name="&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;" class="anchor-navigation-ex-anchor" href="#&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;</h3>
<p>&#x5047;&#x5982;&#x6709;&#x4E2A; Pod &#x9700;&#x8981;&#x5F88;&#x5927;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#xFF0C;&#x800C;&#x96C6;&#x7FA4;&#x4E2D;&#x7684;&#x4E0D;&#x540C;&#x673A;&#x5668;&#x7684;&#x786C;&#x76D8;&#x5BB9;&#x91CF;&#x90FD;&#x4E0D;&#x540C;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B; Pod &#x5728;&#x786C;&#x76D8;&#x5F88;&#x5927;&#x7684;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x90E8;&#x7F72;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7ED9;&#x8282;&#x70B9;&#x8BBE;&#x7F6E;&#x6807;&#x7B7E;&#xFF0C;&#x7136;&#x540E;&#x5728; Pod &#x7684; YAML &#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x4E0A;&#x9009;&#x62E9;&#x5668;&#xFF0C;&#x8BA9;  Pod &#x81EA;&#x52A8;&#x67E5;&#x627E;&#x8FD9;&#x7C7B;&#x5BB9;&#x91CF;&#x5927;&#x7684;&#x8282;&#x70B9;&#xFF0C;&#x5E76;&#x90E8;&#x7F72;&#x3002;</p>
<p>&#x83B7;&#x53D6;&#x96C6;&#x7FA4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x8282;&#x70B9;&#x7684; Label&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl get nodes --show-labels
</code></pre>
<pre class="language-"><code class="lang-bash">root@master:~<span class="token comment"># kubectl get nodes --show-labels</span>
NAME     STATUS   ROLES                  AGE   VERSION   LABELS
master   Ready    control-plane,master   47h   v1.22.2   beta.kubernetes.io/arch<span class="token operator">=</span>amd64,beta.kubernetes.io/os<span class="token operator">=</span>linux,kubernetes.io/arch<span class="token operator">=</span>amd64,kubernetes.io/hostname<span class="token operator">=</span>master,kubernetes.io/os<span class="token operator">=</span>linux,node-role.kubernetes.io/control-plane<span class="token operator">=</span>,node-role.kubernetes.io/master<span class="token operator">=</span>,node.kubernetes.io/exclude-from-external-load-balancers<span class="token operator">=</span>
slave1   Ready    <span class="token operator">&lt;</span>none<span class="token operator">&gt;</span>                 47h   v1.22.2   beta.kubernetes.io/arch<span class="token operator">=</span>amd64,beta.kubernetes.io/os<span class="token operator">=</span>linux,kubernetes.io/arch<span class="token operator">=</span>amd64,kubernetes.io/hostname<span class="token operator">=</span>slave1,kubernetes.io/os<span class="token operator">=</span>linux
</code></pre>
<blockquote>
<p>&#x6211;&#x4EEC;&#x7684;&#x8282;&#x70B9;&#x540D;&#x79F0;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x8BFB;&#x8005;&#x6309;&#x7167;&#x5B9E;&#x9645;&#x540D;&#x79F0;&#x4FEE;&#x6539;&#x540E;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x3002;</p>
</blockquote>
<p>&#x7ED9;&#x8282;&#x70B9;&#x52A0;&#x4E0A;&#x6807;&#x7B7E;&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl label node instance-2 <span class="token assign-left variable">disksize</span><span class="token operator">=</span>big
</code></pre>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x5728;&#x7F16;&#x5199; yaml &#x6587;&#x4EF6;&#x65F6;&#xFF0C;&#x5E0C;&#x671B;&#x8FD9;&#x4E2A; pod &#x5728;&#x5BB9;&#x91CF;&#x5927;&#x7684; Node &#x4E0A;&#x8FD0;&#x884C;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199;&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">  nodeSelector:
    <span class="token assign-left variable">disksize</span><span class="token operator">=</span>big
</code></pre>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x6765;&#x7EC3;&#x4E60;&#x4E00;&#x4E0B;&#xFF0C;&#x5148;&#x5220;&#x9664;&#x4EE5;&#x524D;&#x7684; Deployment&#xFF1A;<code>kubectl delete deployment nginx</code>&#x3002;</p>
<p>&#x7136;&#x540E;&#x6211;&#x4EEC;&#x4F7F;&#x7528; YAML &#x521B;&#x5EFA;&#x4E00;&#x4E2A; Deployment&#xFF0C;&#x4FDD;&#x5B58;&#x4E0B;&#x9762; YAML &#x5230; nginx.yaml &#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> apps/v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Deployment
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>latest
        <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
      <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
        <span class="token key atrule">disksize</span><span class="token punctuation">:</span> <span class="token string">&quot;big&quot;</span>
</code></pre>
<p>&#x6267;&#x884C; <code>kubectl apply -f nginx.yaml</code> &#xFF0C;&#x90E8;&#x7F72; Pod&#x3002;</p>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x7531;&#x4E8E;&#x662F; Deployment &#x7684;&#xFF0C;selector &#x548C;&#x522B;&#x7684;&#x5B57;&#x6BB5;&#x662F;&#x5FC5;&#x586B;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x770B;&#x8D77;&#x6765;&#x5185;&#x5BB9;&#x6BD4;&#x8F83;&#x591A;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x91CC;&#x6838;&#x5FC3;&#x662F; <code>nodeSelector</code>&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;&#x65F6;&#xFF0C;&#x5728; spec &#x4E0B;&#x9762;&#x589E;&#x52A0; <code>nodeSelector</code>&#xFF0C;&#x5176;&#x4F4D;&#x7F6E;&#x5982;&#x4E0B; YAML &#x6240;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">    <span class="token key atrule">spec</span><span class="token punctuation">:</span>
      <span class="token key atrule">containers</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>latest
        <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
      <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
        <span class="token key atrule">disksize</span><span class="token punctuation">:</span> <span class="token string">&quot;big&quot;</span>
</code></pre>
<p>&#x987A;&#x4FBF;&#x804A;&#x4E00;&#x4E0B;&#x5B98;&#x65B9;&#x7684;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;&#x8BBE;&#x7F6E; Node &#x7684; Label&#xFF0C;&#x8868;&#x793A;&#x786C;&#x76D8;&#x662F; ssd&#x3002;</p>
<pre class="language-"><code class="lang-shell">kubectl label nodes node-1 <span class="token assign-left variable">disktype</span><span class="token operator">=</span>ssd
</code></pre>
<p>&#x5728; yaml &#x6587;&#x4EF6;&#x7684;&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;&#x4E2D;&#xFF0C;&#x6DFB;&#x52A0;&#x9009;&#x62E9;&#x3002;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
  <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
    <span class="token key atrule">disktype</span><span class="token punctuation">:</span> ssd
</code></pre>
<p>&#x4F7F;&#x7528; Pod &#x9009;&#x62E9;&#x8282;&#x70B9;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">labels</span><span class="token punctuation">:</span>
    <span class="token key atrule">env</span><span class="token punctuation">:</span> test
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx
    <span class="token key atrule">imagePullPolicy</span><span class="token punctuation">:</span> IfNotPresent
  <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
    <span class="token key atrule">disktype</span><span class="token punctuation">:</span> ssd
</code></pre>
<h3 id="&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;"><a name="&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;" class="anchor-navigation-ex-anchor" href="#&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x547D;&#x4EE4;&#x5F0F;&#x6807;&#x7B7E;&#x9009;&#x62E9;</h3>
<p>&#x5728;&#x524D;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x5B66;&#x4E60;&#x4E86; nodeSelector &#xFF0C;&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x9009;&#x62E9;&#x5408;&#x9002;&#x7684; Node &#x8FD0;&#x884C; Pod&#xFF0C;&#x5B9E;&#x9645;&#x4E0A; Kubernets &#x7684;&#x6807;&#x7B7E;&#x9009;&#x62E9;&#x662F;&#x4E30;&#x5BCC;&#x591A;&#x6837;&#x7684;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">  <span class="token key atrule">nodeSelector</span><span class="token punctuation">:</span>
    <span class="token key atrule">disktype</span><span class="token punctuation">:</span> ssd
    <span class="token key atrule">disksize</span><span class="token punctuation">:</span> big
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x8868;&#x793A;&#x8282;&#x70B9;&#x9009;&#x62E9;&#x5668;&#x662F;&#x7B49;&#x503C;&#x9009;&#x62E9;&#xFF0C;&#x8868;&#x8FBE;&#x5F0F;&#x662F; <code>disktype=ssd &amp;&amp; disksize=big</code>&#xFF0C;&#x8282;&#x70B9;&#x9700;&#x8981;&#x5177;&#x5907;&#x8FD9;&#x4E24;&#x4E2A; Label (key&#x548C;value&#x90FD;&#x8981;&#x7B26;&#x5408;)&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x7C7B;&#x4F3C;&#x65B9;&#x5F0F;&#x7B5B;&#x9009;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684; Pod&#x3002;</p>
<p>&#x67E5;&#x8BE2; Pod &#x6240;&#x6709;&#x7684; Label&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl get pods --show-labels
</code></pre>
<p>&#x67E5;&#x627E;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684; pod&#xFF08;&#x53C2;&#x8003; LABELS &#x5B57;&#x6BB5;&#xFF0C;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x91CC;&#x9762;&#x7684;&#x6807;&#x7B7E;&#x9009;&#x62E9;&#xFF09;&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl get pods -l <span class="token assign-left variable">app</span><span class="token operator">=</span>nginx
kubectl get pods -l app<span class="token operator">!=</span>nginx
</code></pre>
<p>&#x5217;&#x51FA;&#x4E0D;&#x5305;&#x542B;&#x67D0;&#x4E2A;&#x6807;&#x7B7E;&#x7684; Pod&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl get pods -l <span class="token string">&apos;!env&apos;</span>
kubectl get pods -l <span class="token string">&apos;!app&apos;</span>
</code></pre>
<p>&#x83B7;&#x53D6;&#x540C;&#x65F6;&#x5305;&#x542B;&#x4E24;&#x4E2A;&#x6807;&#x7B7E;&#x7684; Pod&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl get pods -l app,env
</code></pre>
<p>&#x6807;&#x7B7E;&#x9009;&#x62E9;&#x6709;&#x7B49;&#x503C;&#x548C;&#x96C6;&#x5408;&#x4E24;&#x79CD;&#xFF0C;&#x5176;&#x4E2D;&#x7B49;&#x503C;&#x9009;&#x62E9;&#x6709; <code>=</code>&#x3001;<code>==</code>&#x3001;<code>!=</code> &#x4E09;&#x79CD;&#x7B26;&#x53F7;&#xFF0C;<code>=</code> &#x548C; <code>==</code> &#x65E0;&#x4EFB;&#x4F55;&#x533A;&#x522B;&#xFF0C;&#x6240;&#x4EE5;&#x5B9E;&#x9645;&#x53EA;&#x6709; <code>&#x7B49;&#x4E8E;</code> &#x3001;<code>&#x4E0D;&#x7B49;&#x4E8E;</code> &#x4E24;&#x79CD;&#x9009;&#x62E9;&#x60C5;&#x51B5;&#x3002;</p>
<p>&#x5728;&#x591A;&#x4E2A;&#x9700;&#x6C42;(&#x591A;&#x4E2A;label)&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>&amp;&amp;</code> &#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x8868;&#x793A;&#x9700;&#x8981;&#x540C;&#x65F6;&#x7B26;&#x5408;&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#xFF0C;&#x4F46;&#x662F;&#x9009;&#x62E9;&#x5668;&#x4E0D;&#x5B58;&#x5728; <code>||</code> &#x8FD9;&#x79CD;&#x903B;&#x8F91;&#x6216;&#x8FD0;&#x7B97;&#x7B26;&#x3002;</p>
<p>&#x67E5;&#x770B;&#x7B26;&#x5408;&#x4E24;&#x4E2A;&#x6761;&#x4EF6;&#x7684; &#x8282;&#x70B9;&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl get nodes -l <span class="token assign-left variable">disktype</span><span class="token operator">=</span>ssd,disksize<span class="token operator">!=</span>big
<span class="token comment"># &#x591A;&#x4E2A;&#x6761;&#x4EF6;&#x4F7F;&#x7528; &#x9017;&#x53F7;&quot;,&quot;&quot; &#x9694;&#x5F00;&#xFF0C;&#x800C;&#x4E0D;&#x662F; &quot;&amp;&amp;&quot;&#x3002;</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x591A;&#x6761;&#x4EF6;&#x4F7F;&#x7528; <code>,</code> &#x9694;&#x5F00;&#xFF0C;&#x800C;&#x4E0D;&#x662F; <code>&amp;&amp;</code>&#xFF0C;&#x4E0D;&#x80FD;&#x5199;&#x6210; <code>disktype=ssd &amp;&amp; disksize!=big</code>&#x3002;</p>
</blockquote>
<p>&#x5BF9;&#x4E8E;&#x96C6;&#x5408;&#x9009;&#x62E9;&#x65B9;&#x5F0F;&#xFF0C;&#x652F;&#x6301;&#x4E09;&#x79CD;&#x64CD;&#x4F5C;&#x7B26;&#xFF1A;<code>in</code>&#x3001;<code>notin</code> &#x548C; <code>exists</code>&#x3002;&#x4E0D;&#x8FC7;&#x522B;&#x7406;&#x89E3;&#x6210;&#x662F;&#x4ECE;&#x96C6;&#x5408;&#x4E2D;&#x9009;&#x62E9;&#xFF0C;&#x4E0B;&#x9762;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<p>&#x5047;&#x5982;&#x6709;&#x4E09;&#x4E2A; Node&#xFF0C;&#x5176; disksize &#x6709; big&#x3001;medium&#x3001;small &#x4E09;&#x79CD;&#x7C7B;&#x578B;&#x3002;</p>
<pre class="language-"><code class="lang-text">NAME        LABELS
node1      disksize=big
node2      disksize=medium
node3      disksize=small
</code></pre>
<p>&#x6211;&#x4EEC;&#x8981;&#x90E8;&#x7F72;&#x4E00;&#x4E2A; pod&#xFF0C;&#x5728; big&#x3001;medium &#x4E2D;&#x90FD;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x5199;&#x6210;</p>
<pre class="language-"><code class="lang-shell">kubectl get nodes -l disksize <span class="token keyword">in</span> <span class="token punctuation">(</span>big,medium<span class="token punctuation">)</span>
<span class="token comment"># ... -l disksize in (big,medium)</span>
</code></pre>
<pre class="language-"><code class="lang-shell">kubectl get nodes -l disksize notin <span class="token punctuation">(</span>small<span class="token punctuation">)</span>
<span class="token comment"># ... -l disksize notin (small)</span>
<span class="token comment"># &#x4E0D;&#x5728; small &#x4E2D;&#x8FD0;&#x884C;</span>
</code></pre>
<p>&#x800C; exists &#x5219;&#x8DDF; <code>!=</code> &#x7C7B;&#x4F3C;&#xFF0C;&#x4F46;&#x662F; exists &#x8868;&#x793A;&#x53EA;&#x8981;&#x5B58;&#x5728;&#x8FD9;&#x4E2A; label &#x5373;&#x53EF;&#xFF0C;&#x800C;&#x4E0D;&#x8BBA;&#x5176;&#x8BBE;&#x7F6E;&#x4E86;&#x662F;&#x4EC0;&#x4E48;&#x503C;&#x3002;</p>
<pre class="language-"><code class="lang-shell">kubectl get nodes -l exists disksize
<span class="token comment"># -l exists disksize</span>
<span class="token comment"># &#x7B49;&#x540C; -l disksize in (big,medium,small)</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>&apos;&apos;</code> &#x628A;&#x9009;&#x62E9;&#x8868;&#x8FBE;&#x5F0F;&#x5305;&#x8D77;&#x6765;&#x3002;</p>
<pre class="language-"><code class="lang-shell">kubectl get pods -l <span class="token string">&apos;app=nginx&apos;</span>
</code></pre>
<blockquote>
<p>&#x76EE;&#x524D;&#x63D0;&#x5230;&#x7684;&#x6807;&#x7B7E;&#x9009;&#x62E9;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x547D;&#x4EE4;&#x5F0F;&#x9009;&#x62E9;&#xFF0C;&#x6BD4;&#x8F83;&#x5BB9;&#x6613;&#x4F7F;&#x7528;&#xFF0C;&#x800C;&#x4E14;&#x652F;&#x6301;&#x7684;&#x9009;&#x62E9;&#x65B9;&#x5F0F;&#x6BD4;&#x8F83;&#x591A;&#xFF1B;&#x4F46;&#x662F;&#x5199;&#x8FDB; YAML &#x6216; JSON &#x4E2D;&#x7684;&#x9009;&#x62E9;&#x5668;&#xFF0C;&#x5219;&#x7F16;&#x5199;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x652F;&#x6301;&#x6240;&#x6709;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#x3002;</p>
</blockquote>
<h3 id="selector"><a name="selector" class="anchor-navigation-ex-anchor" href="#selector"><i class="fa fa-link" aria-hidden="true"></i></a>selector</h3>
<p>&#x524D;&#x9762;&#x5DF2;&#x7ECF;&#x63D0;&#x5230;&#x4E86; YAML &#x7684; <code>nodeSelector</code> &#x548C; &#x547D;&#x4EE4;&#x5F0F;&#x7684;&#x9009;&#x62E9;&#xFF0C;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4ECB;&#x7ECD; YAML &#x7684; <code>selector</code>&#x3002;</p>
<p>&#x5728; Deployment &#x7684; YAML &#x4E2D;&#xFF0C;&#x6709;&#x8FD9;&#x6837;&#x7684;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
      <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">template</span><span class="token punctuation">:</span>
    <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
      <span class="token key atrule">labels</span><span class="token punctuation">:</span>
        <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
</code></pre>
<blockquote>
<p>&#x7531; Deployment &#x90E8;&#x7F72;&#x7684;&#x6BCF;&#x4E2A; Pod &#x7684;&#x540D;&#x79F0;&#x90FD;&#x4E0D;&#x540C;(&#x968F;&#x673A;&#x751F;&#x6210;)&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x53EF;&#x80FD;&#x4EE5; Pod &#x540D;&#x79F0;&#x4F5C;&#x4E3A;&#x8868;&#x793A;&#xFF0C;&#x4F55;&#x51B5; Pod &#x53EA;&#x662F;&#x4E34;&#x65F6;&#x6027;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x4F1A;&#x4E3A;&#x6BCF;&#x4E2A; Pod &#x52A0;&#x4E0A;&#x4E00;&#x4E2A;&#x76F8;&#x540C;&#x7684; label&#xFF0C;&#x8868;&#x793A;&#x5B83;&#x4EEC;&#x90FD;&#x662F;&#x540C;&#x4E00;&#x4E2A; Deployment &#x521B;&#x5EFA;&#x7684;&#x3002;</p>
<p>YAML &#x6587;&#x4EF6;&#x914D;&#x7F6E;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">template</span><span class="token punctuation">:</span>
 <span class="token key atrule">metadata</span><span class="token punctuation">:</span>
   <span class="token key atrule">labels</span><span class="token punctuation">:</span>
     <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
</code></pre>
</blockquote>
<p>&#x5176;&#x4E2D; <code>spec.template</code> &#x662F;&#x5B9A;&#x4E49;&#x6240;&#x6709; Pod &#x7684;&#x6A21;&#x677F;&#xFF0C;<code>template.metadata</code> &#x53EF;&#x4EE5;&#x4E3A;&#x6240;&#x6709; Pod &#x8BBE;&#x7F6E;&#x4E00;&#x4E9B;&#x5143;&#x6570;&#x636E;&#xFF0C;&#x4F8B;&#x5982; <code>labels</code>&#xFF0C;<strong>&#x5BF9;&#x4E8E; Deployment &#x7B49;&#x63A7;&#x5236;&#x5668;&#xFF0C;&#x8BBE;&#x7F6E;&#x6807;&#x7B7E;&#x662F;&#x5FC5;&#x4E0D;&#x53EF;&#x5C11;&#x7684;</strong>&#x3002;</p>
<p>&#x63A5;&#x4E0B;&#x6765;&#x662F; <code>spec.selector</code>&#xFF0C;<code>selector</code> &#x5B57;&#x6BB5;&#x5B9A;&#x4E49; Deployment &#x5982;&#x4F55;&#x67E5;&#x627E;&#x8981;&#x7BA1;&#x7406;&#x7684; Pods&#x3002;<code>spec.selector</code> &#x6709;&#x4E2A; <code>matchLabels</code> &#x5B57;&#x6BB5;&#xFF0C;&#x6BCF;&#x4E2A; Deployment &#x90FD;&#x8981;&#x914D;&#x7F6E; <code>matchLabels</code>&#x3002;<code>matchLabels</code> &#x91CC;&#x9762;&#x7684;&#x6807;&#x7B7E;&#x662F;&#x96C6;&#x5408;&#x8FD0;&#x7B97;&#x7684; <code>in</code>&#xFF0C;Pod &#x53EA;&#x9700;&#x8981;&#x5305;&#x542B;&#x8FD9;&#x4E9B;&#x6807;&#x7B7E;&#xFF0C;&#x5C31;&#x4F1A;&#x88AB;&#x6B64; Deployment &#x7BA1;&#x7406;&#x3002;</p>
<p>&#x5728;&#x4E4B;&#x524D;&#x7684;&#x521B;&#x5EFA;&#x7684; Deployment(nginx) &#xFF0C;&#x5176; Pod &#x90FD;&#x4F1A;&#x5E26;&#x4E0A; <code>app=nginx</code> &#x7684;&#x6807;&#x7B7E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x521B;&#x5EFA; Pod &#x4E4B;&#x524D;&#xFF0C;&#x8BBE;&#x7F6E; Pod &#x7684;&#x6807;&#x7B7E;&#x3002; Deployment &#x7684; YAML &#x6587;&#x4EF6;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-YAML">apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx666
  template:
    metadata:
      labels:
        app: nginx666
        type: test
    spec:
      containers:
      - image: nginx:latest
        name: nginx
</code></pre>
<p>&#x67E5;&#x8BE2;&#x521B;&#x5EFA;&#x7684; Pod&#xFF1A;</p>
<pre class="language-"><code>NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-64b465ddc9-8t9c8   1/1     Running   0          23s   app=nginx666,pod-template-hash=64b465ddc9,type=test
</code></pre><p>&#x540C;&#x6837;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x521B;&#x5EFA; Service &#x6216;&#x8005;&#x4F7F;&#x7528; ReplicationController &#x65F6;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x6807;&#x7B7E;&#x9009;&#x62E9;&#x5408;&#x9002;&#x7684; Pod&#x3002;</p>
<p>&#x56DE;&#x5FC6;&#x4E00;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x5DF2;&#x7ECF;&#x5B66;&#x4E60;&#x8FC7;&#x600E;&#x4E48;&#x521B;&#x5EFA; Service&#xFF0C;&#x793A;&#x4F8B;&#x547D;&#x4EE4;&#xFF1A;</p>
<pre class="language-"><code class="lang-shell">kubectl expose deployment xxx <span class="token punctuation">..</span>.
</code></pre>
<p>&#x547D;&#x4EE4;&#x5F0F;&#x7684;&#x9009;&#x62E9;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x52A0;&#x4E0A; <code>deployment xxx</code> &#x5373;&#x53EF;&#x3002;&#x5BF9;&#x4E8E; YAML &#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>seletcor</code> &#x9009;&#x62E9;&#x5668;&#xFF0C;&#x8868;&#x793A;&#x8BE5; Service &#x5728;&#x4EC0;&#x4E48;&#x5BF9;&#x8C61;&#x4E0A;&#x8D77;&#x6548;&#x3002;</p>
<p>&#x5047;&#x5982;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x90E8;&#x7F72;&#x4E86; nginx&#xFF0C;&#x90A3;&#x4E48;&#x67E5;&#x8BE2; <code>kubectl get pods --show-labels</code> &#x65F6;&#xFF0C;&#x5176; Pod &#x7684; LABELS &#x4F1A;&#x6709; <code>app=nginx</code>&#xFF0C;&#x90A3;&#x4E48;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x9009;&#x62E9;&#xFF1A;</p>
<pre class="language-"><code class="lang-YAML">  selector:
    app: nginx
</code></pre>
<p>&#x4F7F;&#x7528; Service &#x65F6;&#xFF0C;&#x8981;&#x7ED1;&#x5B9A;&#x5177;&#x4F53;&#x7684; Pod&#xFF0C;&#x5B8C;&#x6574;&#x7684; YAML &#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Service
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> my<span class="token punctuation">-</span>service
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">type</span><span class="token punctuation">:</span> LoadBalancer
  <span class="token key atrule">selector</span><span class="token punctuation">:</span>
    <span class="token key atrule">app</span><span class="token punctuation">:</span> nginx
  <span class="token key atrule">ports</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token key atrule">protocol</span><span class="token punctuation">:</span> TCP
      <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">80</span>
      <span class="token key atrule">targetPort</span><span class="token punctuation">:</span> <span class="token number">6666</span>
<span class="token key atrule">status</span><span class="token punctuation">:</span>
  <span class="token key atrule">loadBalancer</span><span class="token punctuation">:</span>
    <span class="token key atrule">ingress</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token key atrule">ip</span><span class="token punctuation">:</span> 192.0.2.127
</code></pre>
<h3 id="&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;"><a name="&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;" class="anchor-navigation-ex-anchor" href="#&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9009;&#x62E9;&#x5668;&#x7684;&#x590D;&#x5408;&#x8FD0;&#x7B97;</h3>
<p>nodeSelector&#x3001;selector &#x8FD8;&#x652F;&#x6301;&#x4EE5;&#x4E0B;&#x9009;&#x62E9;&#x65B9;&#x5F0F; <code>matchLabels</code>&#x3001;<code>matchExpressions</code>&#xFF0C;&#x5728;&#x524D;&#x9762;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E86;&#x89E3; Deployment &#x6709; <code>matchLabels</code> &#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x7EE7;&#x7EED;&#x6DF1;&#x5165;&#x4E86;&#x89E3;&#x3002;</p>
<p><code>matchExpressions</code> &#x662F; Pod &#x9009;&#x62E9;&#x7B97;&#x7B26;&#x9700;&#x6C42;&#x7684;&#x5217;&#x8868;&#x3002; &#x6709;&#x6548;&#x7684;&#x8FD0;&#x7B97;&#x7B26;&#x5305;&#x62EC; <code>In</code>&#x3001;<code>NotIn</code>&#x3001;<code>Exists</code> &#x548C; <code>DoesNotExist</code>&#x3002; &#x6765;&#x81EA; <code>matchLabels</code> &#x548C; <code>matchExpressions</code> &#x7684;&#x6240;&#x6709;&#x8981;&#x6C42;&#x90FD;&#x6309;&#x903B;&#x8F91;&#x4E0E;&#x7684;&#x5173;&#x7CFB;&#x7EC4;&#x5408;&#x5230;&#x4E00;&#x8D77; -- &#x5B83;&#x4EEC;&#x5FC5;&#x987B;&#x90FD;&#x6EE1;&#x8DB3;&#x624D;&#x80FD;&#x5339;&#x914D;&#x3002;</p>
<blockquote>
<p>&#x5728; <code>In</code> &#x548C; <code>NotIn</code> &#x6807;&#x7B7E;&#x4E2D;&#xFF0C;&#x8BBE;&#x7F6E;&#x7684;&#x503C;&#x5FC5;&#x987B;&#x662F;&#x975E;&#x7A7A;&#x7684;&#x3002;</p>
<p>Exists&#x3001;DoesNotExist &#x8868;&#x793A;&#x67D0;&#x4E2A;&#x6807;&#x7B7E;&#x662F;&#x5426;&#x5B58;&#x5728;&#xFF1B;</p>
<p>In&#x3001;NotIn &#x8868;&#x793A; A &#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6807;&#x7B7E;&#x662F;&#x5426;&#x90FD;&#x5728; B &#x96C6;&#x5408;&#x4E2D;&#x3002; </p>
</blockquote>
<p><code>matchExpressions</code> &#x7684;&#x8868;&#x8FBE;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml">  <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">key</span><span class="token punctuation">:</span> tier<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>cache1<span class="token punctuation">,</span>cache2<span class="token punctuation">]</span><span class="token punctuation">}</span>
    <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">key</span><span class="token punctuation">:</span> environment<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> NotIn<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>dev<span class="token punctuation">]</span><span class="token punctuation">}</span>
    <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">Key</span><span class="token punctuation">:</span> disksize<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> Exists<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>Pod &#x5FC5;&#x987B;&#x6709; tier &#x8FD9;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x5E76;&#x4E14;&#x503C;&#x5FC5;&#x987B;&#x5728; [cache1,cach2] &#x4E2D;&#x627E;&#x5230;&#x3002;</p>
<p>Pod &#x5FC5;&#x987B;&#x6709; environment &#x8FD9;&#x4E2A;&#x6807;&#x7B7E;&#xFF0C;&#x5E76;&#x4E14;&#x503C;&#x4E0D;&#x80FD;&#x4E3A; <code>dev</code>&#xFF0C;&#x6216;&#x8005;&#x8BF4;&#x4E0D;&#x80FD;&#x5728; [dev] &#x4E2D;&#x51FA;&#x73B0;&#x3002;</p>
<p>Pod &#x5FC5;&#x987B;&#x5B58;&#x5728; disksize &#x8FD9;&#x4E2A;&#x6807;&#x7B7E;&#x3002;</p>
</blockquote>
<p>&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; YAML &#x683C;&#x5F0F;&#x8868;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-YAML">        - matchExpressions:
          - key: kubernetes.io/e2e-az-name
            operator: In
            values:
            - e2e-az1
            - e2e-az2
</code></pre>
<blockquote>
<p>&#x8BFB;&#x8005;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x81EA;&#x5DF1;&#x559C;&#x597D;&#x4F7F;&#x7528;&#x5176;&#x4E2D;&#x4E00;&#x79CD;&#x7F16;&#x5199;&#x683C;&#x5F0F;&#x3002;</p>
</blockquote>
<p><code>matchLabels</code> &#x662F;&#x7531; <code>{key,value}</code> &#x5BF9;&#x7EC4;&#x6210;&#x7684;&#x6620;&#x5C04;&#x3002; <code>matchLabels</code> &#x6620;&#x5C04;&#x4E2D;&#x7684;&#x5355;&#x4E2A; <code>{key,value }</code> &#x7B49;&#x540C;&#x4E8E; <code>matchExpressions</code> &#x7684;&#x5143;&#x7D20;&#xFF0C; &#x5176; <code>key</code> &#x5B57;&#x6BB5;&#x4E3A; &quot;key&quot;&#xFF0C;<code>operator</code> &#x4E3A; &quot;In&quot;&#xFF0C;&#x800C; <code>values</code> &#x6570;&#x7EC4;&#x4EC5;&#x5305;&#x542B; &quot;value&quot;&#x3002;</p>
<blockquote>
<p>&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;&#xFF0C;<code>matchLabels</code> &#x793A;&#x4F8B;&#x4E3A;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"> <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
   <span class="token key atrule">component</span><span class="token punctuation">:</span> redis
</code></pre>
<p>&#x76F8;&#x5BF9;&#x4E8E; <code>matchExpressions</code> &#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"> <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
   <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">key</span><span class="token punctuation">:</span> component<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>redis<span class="token punctuation">]</span><span class="token punctuation">}</span>
</code></pre>
</blockquote>
<p>&#x4E24;&#x79CD;&#x9009;&#x62E9;&#x5668;&#x53EF;&#x4EE5;&#x7ED3;&#x5408;&#x4F7F;&#x7528;&#xFF0C;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-yaml"><span class="token key atrule">selector</span><span class="token punctuation">:</span>
  <span class="token key atrule">matchLabels</span><span class="token punctuation">:</span>
    <span class="token key atrule">component</span><span class="token punctuation">:</span> redis
  <span class="token key atrule">matchExpressions</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">key</span><span class="token punctuation">:</span> tier<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> In<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>cache<span class="token punctuation">]</span><span class="token punctuation">}</span>
    <span class="token punctuation">-</span> <span class="token punctuation">{</span><span class="token key atrule">key</span><span class="token punctuation">:</span> environment<span class="token punctuation">,</span> <span class="token key atrule">operator</span><span class="token punctuation">:</span> NotIn<span class="token punctuation">,</span> <span class="token key atrule">values</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>dev<span class="token punctuation">]</span><span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p><strong>[Info] &#x63D0;&#x793A;</strong></p>
<p>&#x590D;&#x5408;&#x9009;&#x62E9;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x53EA;&#x9700;&#x4E86;&#x89E3;&#x5373;&#x53EF;&#x3002;</p>
</blockquote>
<h2 id="&#x6CE8;&#x89E3;"><a name="&#x6CE8;&#x89E3;" class="anchor-navigation-ex-anchor" href="#&#x6CE8;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6CE8;&#x89E3;</h2>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; Kubernetes &#x6CE8;&#x89E3;&#x4E3A;&#x5BF9;&#x8C61;&#x9644;&#x52A0;&#x4EFB;&#x610F;&#x7684;&#x975E;&#x6807;&#x8BC6;&#x7684;&#x5143;&#x6570;&#x636E;&#xFF0C;&#x6CE8;&#x89E3;&#x4F7F;&#x7528; annotations &#x6807;&#x8BC6;&#x3002;&#x5BA2;&#x6237;&#x7AEF;&#x7A0B;&#x5E8F;&#xFF08;&#x4F8B;&#x5982;&#x5DE5;&#x5177;&#x548C;&#x5E93;&#xFF09;&#x80FD;&#x591F;&#x83B7;&#x53D6;&#x8FD9;&#x4E9B;&#x5143;&#x6570;&#x636E;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x67E5;&#x770B; dashboard &#x7684;&#x76F8;&#x5173; annotations :</p>
<pre class="language-"><code class="lang-bash">kubectl describe services -n kubernetes-dashboard
</code></pre>
<pre class="language-"><code>... ...
Labels:                   k8s-app=kubernetes-dashboard
Annotations:              <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>none</span><span class="token punctuation">&gt;</span></span>
... ...
</code></pre><p>annotations &#x7531; key/value &#x7EC4;&#x6210;&#xFF0C;&#x7C7B;&#x4F3C; label&#xFF0C;&#x4F46;&#x662F; annotations &#x652F;&#x6301;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x5B57;&#x7B26;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4F5C;&#x6784;&#x5EFA;&#x53D1;&#x5E03;&#x955C;&#x50CF;&#x65F6;&#x7684;&#x4FE1;&#x606F;&#x3001;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x7B49;&#x3002;</p>
<pre class="language-"><code class="lang-bash">kubectl annotate <span class="token function">service</span> kubernetes-dashboard -n kubernetes-dashboard <span class="token assign-left variable">description</span><span class="token operator">=</span><span class="token string">&apos;my test&apos;</span>
</code></pre>
<pre class="language-"><code>key=description

value=my test
</code></pre><p>&#x91CD;&#x65B0;&#x67E5;&#x770B; describe&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF1A;</p>
<pre class="language-"><code>Annotations:              description: my test
</code></pre><p>&#x5982;&#x679C;&#x8981;&#x8986;&#x76D6; key &#x7684;&#x503C;&#xFF0C;&#x9700;&#x8981;&#x52A0;&#x4E0A; <code>--overwrite</code> &#x3002;</p>
<p>&#x5982;&#x679C;&#x8981;&#x5220;&#x9664;&#x4E00;&#x4E2A; key&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">kubectl annotate <span class="token function">service</span> kubernetes-dashboard description-
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x75F4;&#x8005;&#x5DE5;&#x826F; 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x6700;&#x540E;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2021-11-06 20:00:55
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="6.scale.html" class="navigation navigation-prev " aria-label="Previous page: 3.6 Pod 缩放">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="8.schedule.html" class="navigation navigation-next " aria-label="Next page: 3.8 Pod 调度">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.7.Pod 标签","level":"1.4.8","depth":2,"next":{"title":"3.8 Pod 调度","level":"1.4.9","depth":2,"path":"3.pod/8.schedule.md","ref":"3.pod/8.schedule.md","articles":[]},"previous":{"title":"3.6 Pod 缩放","level":"1.4.7","depth":2,"path":"3.pod/6.scale.md","ref":"3.pod/6.scale.md","articles":[]},"dir":"ltr"},"config":{"plugins":["tbfed-pagefooter","anchor-navigation-ex","mermaid-gb3","insert-logo","chapter-fold","advanced-emoji","github","splitter","alerts","popup","prism","-highlight","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 痴者工良 2021","modify_label":"文档最后更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"prism":{"lang":{"flow":"typescript","shell":"bash"},"ignore":["mermaid","eval-js"],"css":["prismjs/themes/prism.css"],"js":["prismjs/prism.js","prismjs/components.js","prismjs/components/prism-go.min.js","prismjs/components/prism-yaml.min.js","prismjs/components/prism-bash.min.js","prismjs/components/prism-shell-session.min.js"]},"github":{"url":"https://github.com/whuanle/kubernetes"},"livereload":{},"splitter":{},"search":{},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"alerts":{},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"insert-logo":{"style":"background: none; max-height: 50px; min-height: 50px","url":"/images/logo.jpg"}},"theme":"default","author":"痴者工良","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Kubernetes - 痴者工良","language":"zh-hans","links":{"sidebar":{"痴者工良的博客":"https://www.whuanle.cn"}},"gitbook":"*","description":"这是一本关于 Kubernetes 的书，作者 痴者工良"},"file":{"path":"3.pod/7.lable.md","mtime":"2021-11-06T12:00:55.640Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-11-16T23:21:22.294Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

